﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _4Gewinnt
{
    class EngineRandy : Engine
    {
        private Board board;
        /// <summary>
        /// Using a proper RNG (System.Random is crap)
        /// </summary>
        private System.Security.Cryptography.RNGCryptoServiceProvider Rs;
        /// <summary>
        /// The opponent
        /// </summary>
        private GameColor op;
        private int ttw = 4;


        /// <summary>
        /// Initializes game engine Randy. Randy always moves to a random (legal) column
        /// </summary>
        /// <param name="me">The color of the player</param>
        public EngineRandy(GameColor me)
        {
            this.board = new Board(ttw);
            this.Rs = new System.Security.Cryptography.RNGCryptoServiceProvider();
            this._me = me;
            if (_me == GameColor.Blue)
                op = GameColor.Red;
            else
                op = GameColor.Blue;
        }



        /// <summary>
        /// Moves to a random legal column
        /// </summary>
        /// <returns>A random legal column</returns>
        public override int makeMove()
        {
            //to be filled by the cryptographic RNG
            byte[] rnd = new byte[1];
            int moveC;

            //find a random column (from 0..6) until a legal move is found
            do
            {
                Rs.GetBytes(rnd);
                moveC = ((int)rnd[0]) % 7;
            }
            while (!board.isLegalColumn(moveC));

            //move
            board.moveTo(_me, moveC);
            
            //return move
            return moveC;
        }


        /// <summary>
        /// Learns an opponent's move
        /// </summary>
        /// <param name="c">The column the opponent moves to</param>
        public override void update(int c)
        {
            board.moveTo(op, c);
        }


        /// <summary>
        /// Called after each engine's turn. null means "nothing to say"
        /// </summary>
        /// <returns>A message a engine can send after each own turn</returns>
        public override string say()
        {
            return "Randy is confident.";
        }

        /// <summary>
        /// Display the name of this engine
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "Randy";
        }
    }
}
